package
{

	import ca.newcommerce.youtube.data.*;
	import ca.newcommerce.youtube.events.*;
	import ca.newcommerce.youtube.feeds.*;
	import ca.newcommerce.youtube.iterators.*;
	import ca.newcommerce.youtube.webservice.YouTubeClient;

	public class YouTubeDataAPI
	{
	
		protected var _youTubeClient:YouTubeClient;
		protected var _requestId:Number;
		
		private var swfURLArray:Array = new Array();
		private var videoDataArray:Array = new Array();

		public function YouTubeDataAPI()
		{
			// now inside of an init function
			_youTubeClient = YouTubeClient.getInstance();

			// register to list to the events you are interested in
			_youTubeClient.addEventListener(StandardVideoFeedEvent.STANDARD_VIDEO_DATA_RECEIVED, doVideos);

			// do your call.. get the Top Rated videos for the last month
			// results 1 to 10; it returns a requestId
			_requestId = _youTubeClient.getStandardFeed(YouTubeClient.STD_TOP_RATED,
											 YouTubeClient.TIME_MONTH, 
											 1,
											 10);
		}

		public function getMostPopularVideoID():String
		{
			var mostPopularID:String = "unknown";
			
			videoDataArray[0].id;
			
			return mostPopularID;
		}

		protected function doVideos(evt:StandardVideoFeedEvent):void
		{
			// we could test to make sure this is the answer
			// to our request by doing the following:
			// if(_requestId == evt.requestId)

			// get a reference to the feed containing the videos
			var feed:VideoFeed = evt.feed;

			// variable to hold each video retreived on the feed
			var video:VideoData;
			
			
			while(video = feed.next())
			{
				/*
				// trace a newline followed by the video title
				trace("\nvideo title:"+video.title);

				// trace the url to use to embed the flash player with this video playing in it..
				trace("player url:"+video.swfUrl);

				// the view count
				trace("viewCount:"+video.viewCount);

				// the comment count
				trace("commentCount:"+video.commentCount);

				// the duration
				trace("duration:"+video.duration);

				// the author
				trace("author:"+video.authors.first().name);
				*/
				swfURLArray.push(video.swfUrl);
				
				videoDataArray.push( video );
			}

		 }

	}

}